﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 业务流节点服务
    /// </summary>
    public class BizflowNodeService : CrudAppService<BizflowNode, BizflowNodeDto, Guid, BizflowNodeInput, BizflowNodeCreateDto, BizflowNodeUpdateDto>, IBizflowNodeService
    {
        private IRepository<BizflowNode, Guid> _bizflowNodeRepository;
        private IRepository<BizflowConfig, Guid> _bizflowConfigRepository;

        public BizflowNodeService(IRepository<BizflowNode, Guid> bizflowNodeRepository, IRepository<BizflowConfig, Guid> bizflowConfigRepository) : base(bizflowNodeRepository)
        {
            _bizflowNodeRepository = bizflowNodeRepository;
            _bizflowConfigRepository = bizflowConfigRepository;
        }

        /// <summary>
        /// 获取业务流节点
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<BizflowNodeDto>> GetListAsync(BizflowNodeInput input)
        {
            var iQuery = await _bizflowNodeRepository.GetQueryableAsync();

            if (!string.IsNullOrEmpty(input.BizflowCode))
            {
                var bizflowConfig = await _bizflowConfigRepository.GetAsync(b => b.BizflowCode == input.BizflowCode);
                iQuery = iQuery.Where(i => i.BizflowConfigId == bizflowConfig.Id);
            }

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<BizflowNode>, List<BizflowNodeDto>>(iQuery.ToList());

                return new PagedResultDto<BizflowNodeDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<BizflowNode>, List<BizflowNodeDto>>(items.ToList());

                return new PagedResultDto<BizflowNodeDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<BizflowNodeDto> CreateAsync(BizflowNodeCreateDto input)
        {
            BizflowNode processType = ObjectMapper.Map<BizflowNodeCreateDto, BizflowNode>(input);
            processType = await _bizflowNodeRepository.InsertAsync(processType);
            var itemDto = ObjectMapper.Map<BizflowNode, BizflowNodeDto>(processType);

            return itemDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override async Task<BizflowNodeDto> GetAsync(Guid id)
        {
            var processType = await _bizflowNodeRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<BizflowNode, BizflowNodeDto>(processType);

            return itemDto;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<BizflowNodeDto> UpdateAsync(Guid id, BizflowNodeUpdateDto input)
        {
            var processType = await _bizflowNodeRepository.GetAsync(id);
            processType = ObjectMapper.Map(input, processType);
            var itemDto = ObjectMapper.Map<BizflowNode, BizflowNodeDto>(processType);

            return itemDto;
        }
    }
}
